﻿// <copyright file="Pawn.cs" company="ALCPU">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <author>Arthur Liberman</author>
// <email>Arthur_Liberman@hotmail.com</email>
// <date>03-30-2010</date>
// <summary>Contains Pawn definition.</summary>

namespace Chess.Figures
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows.Media;
    using ChessDefinitions;

    /// <summary>
    /// Representative of the Pawn piece
    /// </summary>
    public class Pawn : ChessPiece
    {
        /// <summary>
        /// <c>true</c> if no moves were made yet; <c>false</c> otherwise.
        /// </summary>
        private bool firstMove;

        /// <summary>
        /// Initializes a new instance of the <see cref="Pawn"/> class.
        /// </summary>
        /// <param name="color">The piece color.</param>
        /// <param name="gameboard">The gameboard.</param>
        /// <param name="position">The starting position.</param>
        public Pawn(IPieceColor color, Gameboard gameboard, Position position)
            : base(color, gameboard, position)
        {
            if (color == IPieceColor.Black)
            {
                this.Image = (Brush)App.Current.FindResource("BlackPawn");
            }
            else if (color == IPieceColor.White)
            {
                this.Image = (Brush)App.Current.FindResource("WhitePawn");
            }

            this.firstMove = true;
            this.Gameboard.MovePiece(position, this);
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="Pawn"/> class from being created.
        /// </summary>
        private Pawn()
            : base(0, null, new Position())
        {
        }

        /// <summary>
        /// Moves the piece to the destination.
        /// </summary>
        /// <param name="destination">The destination.</param>
        /// <returns>
        ///     <c>true</c> if successful, <c>false</c> if not.
        /// </returns>
        public override bool MoveTo(Position destination)
        {
            if (base.MoveTo(destination))
            {
                this.firstMove = false;
                return true;
            }

            return false;
        }

        /// <summary>
        /// Attacks the specified target.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <returns>
        ///     <c>true</c> if successful, <c>false</c> if not.
        /// </returns>
        public override bool Attack(Position target)
        {
            if (base.Attack(target))
            {
                this.firstMove = false;
                return true;
            }

            return false;
        }

        /// <summary>
        /// Fills the move options.
        /// </summary>
        /// <returns>List of moves.</returns>
        protected override List<Position> GetMoveOptions()
        {
            List<Position> posList = new List<Position>();

            if (this.Color == IPieceColor.Black)
            {
                if (this.Gameboard.IsSquareFree(new Position(this.Position.X, this.Position.Y + 1)))
                {
                    posList.Add(new Position(this.Position.X, this.Position.Y + 1));

                    if (this.firstMove && this.Gameboard.IsSquareFree(new Position(this.Position.X, this.Position.Y + 2)))
                    {
                        posList.Add(new Position(this.Position.X, this.Position.Y + 2));
                    }
                }
            }
            else if (this.Color == IPieceColor.White)
            {
                if (this.Gameboard.IsSquareFree(new Position(this.Position.X, this.Position.Y - 1)))
                {
                    posList.Add(new Position(this.Position.X, this.Position.Y - 1));

                    if (this.firstMove && this.Gameboard.IsSquareFree(new Position(this.Position.X, this.Position.Y - 2)))
                    {
                        posList.Add(new Position(this.Position.X, this.Position.Y - 2));
                    }
                }
            }

            return posList;
        }

        /// <summary>
        /// Fills the attack list.
        /// </summary>
        /// <returns>List of possible attacks.</returns>
        protected override List<Position> GetAttackList()
        {
            List<Position> posList = new List<Position>();

            if (this.Color == IPieceColor.Black)
            {
                if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X - 1, this.Position.Y + 1), this))
                {
                    posList.Add(new Position(this.Position.X - 1, this.Position.Y + 1));
                }

                if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X + 1, this.Position.Y + 1), this))
                {
                    posList.Add(new Position(this.Position.X + 1, this.Position.Y + 1));
                }
            }
            else if (this.Color == IPieceColor.White)
            {
                if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X - 1, this.Position.Y - 1), this))
                {
                    posList.Add(new Position(this.Position.X - 1, this.Position.Y - 1));
                }

                if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X + 1, this.Position.Y - 1), this))
                {
                    posList.Add(new Position(this.Position.X + 1, this.Position.Y - 1));
                }
            }

            return posList;
        }
    }
}